package com.lagou.controller;

import com.lagou.feign.EmailFeignClient;
import com.lagou.service.CodeService;
import com.lagou.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/code")
@RefreshScope
public class CodeController {

    @Autowired
    private CodeService codeService;

    /**
     * 生成验证码并发送到对应邮箱，0.发送成功 1.发送失败 2.已注册
     * false
     *
     * @param email
     * @return
     */
    @GetMapping("/create/{email}")
    @ResponseBody
    public String create(@PathVariable String email, HttpServletRequest request) {
        String clientIp = request.getHeader("X-Real-IP");
        return codeService.create(email,clientIp);
    }

    /**
     * 校验验证码是否正确，0正确 1错误 2超时 3未发送过验证码 3.请一分钟后再试
     *
     * @param email
     * @param code
     * @return
     */
    @GetMapping("/validate/{email}/{code}")
    @ResponseBody
    public String validate(@PathVariable String email, @PathVariable String code) {
        return codeService.validate(email,code);
    }
}
